<?php 
class salariesAction extends AdminMaster{
	private $strClass = 0;
	public function execute($request){
		$this->data['navigation']	= __('module_candidates',null,'menu').' › '. __('module_candidates_action_salaries',null,'menu');
		$this->formData				= $request->getParameter('MasterSalaries');	//get form data from add new || edit page
		
		//$this->data['list']['resources'] = ;
		
		switch ($this->data['action']){
			case ADMIN_DEFINED::DELETE_RECORD:
				$this->delRecord();
				break;
			case ADMIN_DEFINED::DISABLE:
				$this->disableRecord();
				break;
			case ADMIN_DEFINED::ENABLE:
				$this->enableRecord();
				break;
			case ADMIN_DEFINED::ADD_RECORD:
				//$this->redirect('/admin/vi/faqs/resources');die;
				$this->addRecord();
				break;
			case ADMIN_DEFINED::EDIT_RECORD:
				//$this->redirect('/admin/vi/faqs/resources');die;
				$this->editRecord();
				break;
			case ADMIN_DEFINED::SAVE_ORDER:
				$this->saveOrder();
				break;
			#MORE ACTION HERE
			default:
				$this->filterList();
				break;
		}

		$this->setVar('data', $this->data);
	}
	private function filterList(){
		$this->setAction();
		$this->data['navigation'].=' › '. __('nav_list',null,'table_list');
		
		$this->data['list']			 		= MasterSalariesTable::getDataList($this->data['filter'], $this->data['pagination']);
		
		$this->data['pagination']['total']		= MasterSalariesTable::getDataListCount($this->data['filter']);
		$this->data['pagination']['router']		= 'candidate_salaries';
		$this->data['pagination']['params']		= $this->data['filter'];
		$pag = new PaginationHelper2($this->data['pagination']);
		$this->data['pagination_pager']	= $pag->getPaginations();
		if($this->data['filter']['filter_key'] == '*'){
			$this->data['filter']['filter_key'] = '';
		}
	}
	private function delRecord(){
		MasterSalariesTable::delete($this->data['select_list']);
		$this->filterList();
	}
	private function disableRecord(){
		MasterSalariesTable::disable($this->data['select_list']);
		$this->filterList();
	}
	private function enableRecord(){
		MasterSalariesTable::enable($this->data['select_list']);
		$this->filterList();
	}
	private function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		MasterSalaries::saveSortOrder($sortData);
		$this->filterList();
	}
	/*********************************************************************************
	*
	* ADD NEW
	* 
	**********************************************************************************/
	private function addRecord(){
		$this->data['navigation'].=' › '. __('nav_add_new',null,'table_list');
		$this->data['button_action'] = __('button_add',null,'table_list');
		$this->setTemplate('salariesForm');
		
		$form = new MasterSalariesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$product_id = MasterSalariesTable::insert($this->formData);
				if($product_id > 0){
					$this->data['result']	= __('add_new_success', null, 'master_salaries');
					$form = new MasterSalariesFForm();
				}else{
					$this->data['result']	= sprintf(__('add_new_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
					
			}
		}
		$this->setVar('form', $form);
	}

	/*
	 * EDIT
	*/
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit',null,'table_list');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('button_edit',null,'table_list');
		$this->setTemplate('salariesForm');

		$form = new MasterSalariesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(MasterSalariesTable::update($this->formData)){
					$this->data['result']	= __('edit_success', null, 'master_salaries');
				}else{
					$this->data['result']	= sprintf(__('edit_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}
		}else{
			$data = MasterSalariesTable::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				$this->formData = array(
						'salary_from'				=> $data['salary_from'],
						'salary_to'				=> $data['salary_to'],
						'is_enabled'		=> $data['is_enabled'],
						'_csrf_token'=> $form->getCSRFToken()
				);
				$form->bind($this->formData);

			}
		}
		$this->setVar('form', $form);
	}
	#ACTION FOR EACH ROW
	private function setAction(){
		$action = array();
		$action[]=array(
				'href'	=>	'?action_name='.ADMIN_DEFINED::EDIT_RECORD.'&id=%s',
				'text'	=>	__('action_edit',null,'table_list'),
				'title' =>	__('action_edit',null,'table_list'),
		);
	
		$this->data['action_list']	= $action;
	}
}